package com.kuangxiaoyang.foodapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kuangxiaoyang.foodapp.bean.LoginBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";
    EditText mEtTelephone, mEtPassword;
    Button mBtnLogin, mBtnCancel;
    TextView mTvRegister, mTvForgetPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        initEvent();
    }

    private void initView() {
        mEtTelephone = findViewById(R.id.et_telephone);
        mEtPassword = findViewById(R.id.et_password);
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnCancel = findViewById(R.id.btn_cancel);
        mTvRegister = findViewById(R.id.tv_register);
        mTvForgetPassword = findViewById(R.id.tv_forget_password);
    }

    private void initEvent() {
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String telephone = mEtTelephone.getText().toString();
                final String password = mEtPassword.getText().toString();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        OkHttpClient client = new OkHttpClient();
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("telephone", telephone);
                            jsonObject.put("password", password);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        RequestBody requestBody
                                = RequestBody.create(MediaType.parse("application/json"),
                                jsonObject.toString());
                        Request request = new Request.Builder()
                                .url("http://10.2.64.75:8080/foodserver/user/login")
                                .post(requestBody)
                                .build();
                        Call call = client.newCall(request);
                        try {
                            Response response = call.execute();
                            String data = response.body().string();
                            Gson gson = new Gson();
                            final LoginBean loginBean = gson.fromJson(data, LoginBean.class);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (loginBean.isSuccess()) {
                                        Intent intent = new Intent(LoginActivity.this, FoodListActivity.class);
                                        startActivity(intent);
                                    } else {
                                        Toast.makeText(LoginActivity.this, "用户密码错误!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }


}